package linkedList;

public class LinkedList implements ILinkedListOperation{
    /**
     * 实现无头双向链表*/
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode prev;
        public ListNode(int val) {
            this.val = val;
        }
    }
    /*记录头尾节点*/
    private ListNode head;
    private ListNode tail;

    //add（默认尾插）
    @Override
    public void add(int val) {
        ListNode node = byNode(val);
        if(head==null) {
            this.head = node;
        } else{
            node.prev = this.tail;
            this.tail.next = node;
        }
        this.tail = node;
    }

    //头插
    @Override
    public void addFirst(int val) {
        ListNode node = byNode(val);
        if(head==null) {
            this.tail = node;
        } else{
            node.next = this.head;
            this.head.prev = node;
        }
        this.head = node;
    }

    //任意位置插入
    @Override
    public void addIndex(int index, int val) {
        //先判断index是否合法
        if(index<0 || index>size()) {
            throw new IndexOutBoundsException("索引非法！ index："+index);
        }
        //当index为特定值时，直接调用头插尾插
        if(index==0) {
            addFirst(val);
            return;
        }
        if(index==size()) {
            add(val);
            return;
        }
        //走到此处，说明链表中至少有两个节点
        ListNode node = byNode(val);
        ListNode cur = this.head;
        while(index!=0) {
            cur = cur.next;
            index--;
        }

        node.prev = cur.prev;
        node.next = cur;

        cur.prev.next = node;
        cur.prev = node;
    }

    //删除第一个val值为key的节点
    @Override
    public void remove(int key) {
        if(size()==0) {
            throw new IndexOutBoundsException("非法操作，不能在空链表中删除数据！");
        }
        ListNode del = findNode(key);
        if(del==null) {
            System.out.println("抱歉，链表中没有该值！");
            return;
        }

        if(size()==1) {
            this.head = null;
            this.tail = null;
            return;
        }

        if(del.prev==null) {
            //头删
            this.head = this.head.next;
            this.head.prev=null;
        }else if(del.next==null) {
            //尾删
            this.tail = this.tail.prev;
            this.tail.next = null;
        }else{
            del.next.prev = del.prev;
            del.prev.next = del.next;
        }
/*        ListNode del = this.head;
        while(del!=null) {
            if(del.val == key) {
                if(size()==1) {
                    this.head = null;
                    this.tail = null;
                    return;
                }
                if(del.prev==null) {
                    //头删
                    this.head = this.head.next;
                    this.head.prev=null;
                }else if(del.next==null) {
                    //尾删
                    this.tail = this.tail.prev;
                    this.tail.next = null;
                }else{
                    del.next.prev = del.prev;
                    del.prev.next = del.next;
                }
            } else{
                del = del.next;
            }
        }*/
    }

    @Override
    public void removeAllKey(int key) {
        if(size()==0) {
            throw new IndexOutBoundsException("非法操作，不能在空链表中删除数据！");
        }
        ListNode del = this.head;
        while(del!=null) {
            ListNode nextNode = del.next;
            if(del.val==key) {
                //删除
                if(size()==1) {
                    this.head = null;
                    this.tail = null;
                    return;
                }
                if(del.prev==null) {
                    //头删
                    this.head = this.head.next;
                    this.head.prev=null;
                }else if(del.next==null) {
                    //尾删
                    this.tail = this.tail.prev;
                    this.tail.next = null;
                }else{
                    del.next.prev = del.prev;
                    del.prev.next = del.next;
                }
            }
            del = nextNode;
        }
    }

    //链表长度
    @Override
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while(cur!=null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = this.head;
        while(cur!=null) {
            ListNode nextNode = cur.next;
            cur.prev = null;
            cur.next = null;
            //cur.val = null;
            cur = nextNode;
        }
        this.head = null;
        this.tail = null;
    }

    //打印链表
    @Override
    public void display() {
        ListNode cur = this.head;
        while(cur!=null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //创建一个新节点
    private ListNode byNode(int val) {
        ListNode node = new ListNode(val);
        node.next = node.prev = null;
        return node;
    }

    //找val值==key的节点
    private ListNode findNode(int key) {
        ListNode cur = this.head;
        while(cur!=null) {
            if(cur.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
}
